const fs = require('fs');
const path = require('path');
const {organizeStyles} = require('./整理 less')

const folderPath = './源文件/api.js'
setFile(folderPath)
function setFile (path) {
  fs.readFile(path, 'utf8', (err, data) => {
    if (err) {
      console.error(err);
      return;
    }
    let obj = ''
    let exportContent = ''
    let processedList = []
    const getUrl = (url) => {
      const urlList = url.split('/')
      let text = ''
      let objName = ''
      urlList.forEach((item, index) => {
        if (item.length) {
          objName += item.toUpperCase() +(index === urlList.length - 1 ? '' : '_')
          text +=   '/' + item
        }
      })
      obj += `  ${objName}='${text}',
      `
      return objName
    }
    const getExportContent = (params) => {
      return `
  ${params.mogamivalue ? params.mogamivalue : ''}
  export function ${params.value}Api(${params.paramsType? 'params?: any': ''}) {
    return hyRequest.post<data>({
      // ${params.url}
      url: Api.${params.objName},
      data: ${params.paramsType ? 'params': '{}'},
      ${params.type ? 'isJson: true' : 'isFormData: true'}
    })
  }
      `
    }
    const getExportContentV2 = (params) => {
      return `
  ${params.mogamivalue ? params.mogamivalue : ''}
  export function ${params.value}Api(${params.params}:string|number){
    return hyRequest.post<data>({
      // ${params.url}
      url: Api.${params.objName},
      data: {${params.params}},
      ${params.type ? 'isJson: true' : 'isFormData: true'}
    })
  }
      `
    }
    data = data.replace(new RegExp(/Axios/, 'g'), 'http')
    data = data.replace(new RegExp(/http(\s*)\.(post|get)\(/, 'g'), (_) => {
      if(_.includes('get')) {
        return 'http.get('
      } else {
        return 'http.post('
      }
    })
    data = data.replace(new RegExp(/\w \(/, 'g'), (_)  => {
      _ = _.replace(new RegExp(/\s/, 'g'), '')
      return _
    })
    data = data.replace(new RegExp(/}\s*(\n*\s|\n*)}/, 'g'), (_)  => {
      return `},
      }`
    })
    data.replace(/(\/\*{2}\s*[\s\S]*?\*\/|\/\*[\s\S]*?\*\/|\/\/[\s\S]*?\n)?\s*([\w$]+)\s*\(([\s\S]*?)\)\s*{([\s\S]*?)\},/g, (mogami, mogamivalue, a) => {
      mogami = mogami.replace(/(\w+)\(params\)\s*{[^}]*}/gs, (match, value) => {
        let objName = ''
        let processed = false
        let type = false
        match.replace(new RegExp(/http\.(post|get)\('([^']*)'/, 'g'), (_,a, url, c , d) => {
          processed = true
          type = match.includes('isJSON')
          objName = getUrl(url)
          exportContent += getExportContent({
            url,
            value,
            objName,
            mogamivalue,
            paramsType: true,
            type
          })
        });
        // match.replace(new RegExp(/http\.(post|get)\('([^']*)',\s*(.*),\s*({.*})/, 'g'), (_,a, url, c , d) => {
        //   processed = true
        //   type = match.includes('isJSON')
        //   objName = getUrl(url)
        //   exportContent += getExportContent({
        //     value,
        //     objName,
        //     mogamivalue,
        //     paramsType: true,
        //     type
        //   })
        // });
        if (!processed) {
          processedList.push(value)
        }
        return ''
      });
      mogami = mogami.replace(/(\w+)\(\)\s*{[^}]*}/gs, (match, value) => {
        let processed = false
        let objName = ''
        match.replace(new RegExp(/http\.(post|get)\('([^']*)'/, 'g'), (_,a, url, c , d) => {
          processed = true
          objName = getUrl(url)
          exportContent += getExportContent({
            url,
            value,
            objName,
            mogamivalue
          })
        });
        if (!processed) {
          processedList.push(value)
        }
        return ''
      });
      mogami = mogami.replace(/(\w+)\((\w+)\)\s*{[^}]*}/gs, (match, value,params) => {
        let processed = false
        if (params !== 'params') {
          let objName = ''
          let type = false
          match.replace(new RegExp(/http\.(post|get)\('([^']*)'/, 'g'), (_,a, url, c , d) => {
            if (!(`${d}`.includes('isJSON'))) {
              processed = true
              objName = getUrl(url)
              exportContent += getExportContentV2({
                url,
                value,
                objName,
                mogamivalue,
                params,
                type
              })
            }
          });
          return match
        }
        if (!processed) {
          processedList.push(value)
        }
        return ''
      });
      return mogami
    });
    // interface ApiParams {
    //   code?: number;
    //   msg?: string;
    //   status?: number;
    //   success?: boolean;
    // }
    fs.writeFile('./结果文件/api.ts', ` 
import hyRequest from '@/service/index'
enum Api {
  ${obj}
}
${exportContent}
    `, 'utf8', (err) => {
      if (err) {
        console.error(err);
        return;
      }
    });
  });
}
// 读取 SVG 文件
